package org.edu.jgame.cardgames;

import java.awt.Font;
import java.util.Random;

import org.newdawn.slick.*;
import org.newdawn.slick.font.effects.ColorEffect;


public class Player {
	
	int playerID = 0;
	String name = "Player";
	
	Card[] cards = null;
	int number_chair = 0;	// Position of every player in screen 1-4 in clock direction
	int number_card = 0;	// Number of card in player hand - not in the deck	
	
	
	boolean begin_get_card = false;	// Signal to start state Dealing	
	
	static final int DEALING = 1;	// DEALING state
	static final int MOVING = 2;	// MOVING deck state - Delete
	static final int READY = 3;
	static final int PLAYING = 4;	// PLAYING state
	static final int END = 5;		// END one deal state
	static final int ENDTRICK = 6;	// ENDTRICK is mark the end of one trick
	static final int PASS = 7;
		
	boolean is_lead = false;		// The signal for player to lead a card in a trick
	int idx_lead = -1;				// Index of the card is chosen to lead
	
	int cur_state = 0;				// The variable to show the current state of player in one game
	
	int count = 0;					// Misc ...
	
	int win_chair = 0;				// The position of winner in one TRICK
	
	int[] pass = null;
	
	int direction = 0;
	
	int cur_score = 0;				// The current score in one deal
	int total_score = 0;			// The total score of one player
	UnicodeFont unicodeFont = null;
	Sound sclick = null;
	
	static CardStore store = null;	// The card store source of all Image of card ...
	
	// Constructor of Player with ID and position of that Player	
	@SuppressWarnings("unchecked")
	public Player(String name, int playerID, int number_chair) throws SlickException {
		this.name = name;
		this.playerID = playerID;			// This will be give by the server - a Random integer
		this.number_chair = number_chair;	// The position of player in every client with the player in 1st position and
											// the other in 2-4			
		this.store = new CardStore();
		
		unicodeFont = new UnicodeFont(new Font("Arial", Font.PLAIN, 20));
		unicodeFont.addAsciiGlyphs();
		unicodeFont.getEffects().add(new ColorEffect(java.awt.Color.WHITE));
		unicodeFont.loadGlyphs();
		
		sclick = new Sound("data/click.wav");
		
		name += " " + Integer.toString(number_chair);
		
		pass = new int[3];
		pass[0] = pass[1] = pass[2] = 0;
	}
	
	// To get the store - useless - just for someone needed it
	static public CardStore getCardStore() {
		return store;
	}
	
	// Get player ID to determine who are in lead to the server
	public int getPlayerID() {
		return playerID;
	}
	
	// Get the number chair - Not yet know what to do with it
	public int getPlayerChair() {
		return number_chair;
	}
	
	// Initialize the deck to the player
	public void initCard(int[] cards_value) throws SlickException {
		cards = new Card[cards_value.length];	// Everyone know what it mean
		number_card = cards_value.length;		// this too, Bah		
		
		// Initial time for player
		for(int i = 0; i < cards_value.length; i++) {
			switch(number_chair) {
			case 0:			
				cards[i] = new Card(400, 300, cards_value[i]/10, cards_value[i]%10, 0, false);	// constructor of Card
				break;
			case 1:
				cards[i] = new Card(400, 300, cards_value[i]/10, cards_value[i]%10, 90, false);	// this too
				break;
			case 2:
				cards[i] = new Card(400, 300, cards_value[i]/10, cards_value[i]%10, 180, false); // and this
				break;
			case 3:
				cards[i] = new Card(400, 300, cards_value[i]/10, cards_value[i]%10, -90, false); // and finally this
				break;
			}
		}
	}
	
	// Check whether the card is get to a position
	private boolean isCardMove(int idx_card, float x, float y) {
		// Easy to understand
		if(idx_card != -1 && cards[idx_card].getPositionX() == x && cards[idx_card].getPositionY() == y) {
			return true;
		}
		
		return false;
	}
	
	public void dealCard1() {
		int speed = 3;
		
		for(int i = 0; i < number_card; i++) {
			if(i == 0) {
				switch(number_chair) {
				case 0:
					if(!isCardMove(i, 100+i*50, 540)) {
						cards[i].show();
						moveCardTo(i, 100+i*50, 540, speed);
						cards[i].addrotate(5);
					}
					else {
						cards[i].rotate(0);
					}
					break;
				case 1:
					if(!isCardMove(i, 80, 130+i*20)) {
						cards[i].show();
						moveCardTo(i, 80, 130+i*20, speed);
						cards[i].addrotate(5);
					}
					else {
						cards[i].rotate(90);
					}
					break;
				case 2:
					if(!isCardMove(i, 520-i*20, 80)) {
						cards[i].show();
						moveCardTo(i, 520-i*20, 80, speed);
						cards[i].addrotate(5);
					}
					else {
						cards[i].rotate(180);
					}
					break;
				case 3:
					if(!isCardMove(i, 720, 370-i*20)) {
						cards[i].show();
						moveCardTo(i, 720, 370-i*20, speed);
						cards[i].addrotate(5);
					}
					else {
						cards[i].rotate(-90);
					}
					break;
				}
			}
			else {
				switch(number_chair) {
				case 0:
					if((cards[i].getPositionY() < cards[i-1].getPositionY() - 100) 
							|| (cards[i].getPositionY() != 300 && !isCardMove(i, 100+i*50, 540))) {
						cards[i].show();
						moveCardTo(i, 100+i*50, 540, speed);
						cards[i].addrotate(5);
					}
					if(isCardMove(i, 100+i*50, 540)) {
						cards[i].rotate(0);
					}
					break;
				case 1:
					if((cards[i].getPositionX() > cards[i-1].getPositionX() + 80) 
							|| (cards[i].getPositionX() != 400 && !isCardMove(i, 80, 130+i*20))) {
						cards[i].show();
						moveCardTo(i, 80, 130+i*20, speed);
						cards[i].addrotate(5);
					}
					if(isCardMove(i, 80, 130+i*20)) {
						cards[i].rotate(90);
					}
					break;
				case 2:
					if((cards[i].getPositionY() > cards[i-1].getPositionY() + 60) 
							|| (cards[i].getPositionY() != 300 && !isCardMove(i, 520-i*20, 80))) {
						cards[i].show();
						moveCardTo(i, 520-i*20, 80, speed);
						cards[i].addrotate(5);
					}
					if(isCardMove(i, 520-i*20, 80)) {
						cards[i].rotate(180);
					}
					break;
				case 3:
					if((cards[i].getPositionX() < cards[i-1].getPositionX() - 80) 
							|| (cards[i].getPositionX() != 400 && !isCardMove(i, 720, 370-i*20))) {
						cards[i].show();
						moveCardTo(i, 720, 370-i*20, speed);
						cards[i].addrotate(5);
					}
					if(isCardMove(i, 720, 370-i*20)) {
						cards[i].rotate(-90);
					}
					break;
				}
			}
		}
		
		if(isCardMove(12, 100+12*50, 540) == true || isCardMove(12, 80, 130+12*20) 
				|| isCardMove(12, 520-12*20, 80) || isCardMove(12, 720, 370-12*20)) {
			cur_state = READY;
			if(number_chair == 0) {
				for(int i = 0; i < number_card; i++) {
					cards[i].up();
				}
			}
		}
		
	}
	
	public void inactiveCard() {
		
		if( is_lead == true) {
			int count = 0;
			boolean out_type = true;
			
			if(cards[0].getType() == 1 && cards[0].getValue() == 2) {
				for(int i = 1; i < number_card; i++) {
					cards[i].inactive();
				}
				return;
			}
			
			for (int i = 0; i < number_card; i++) {
				if(cards[i].getType() != GameplayState.getSuit() || (cards[i].getType() == 4 && GameplayState.is_heart_out == false)) {
					cards[i].inactive();
					if(cards[i].getType() != 4) {
						out_type = false;
					}
				}
				else {
					count ++;
				}
			}

			if (count == 0) {
				for (int i = 0; i < number_card; i++) {
					if(GameplayState.getSuit() != 0 || out_type == true) {
						cards[i].active();
					}
					else if(cards[i].getType() != 4 || GameplayState.is_heart_out == true) {
						cards[i].active();
					}					
				}
			}
			else {
				for(int i = 0; i < number_card; i++) {
					if(cards[i].isChosen() && !cards[i].isActive()) {
						cards[i].loseChosen();
					}
				}
			}
		}
	}
	
	// The Action when in PLAYING mode and you highlight a card to choose
	public void focusCard(int mouseX, int mouseY) {
		int idx_focus = -1;
		
		for(int i = 0; i < cards.length; i++) {
			cards[i].losefocus();

			// Check the position of the mouse with the position of the
			// current card
			if ((i != idx_lead)
					&& (mouseX >= cards[i].getRPositionX() && mouseX <= cards[i]
							.getRPositionX() + cards[i].getImage().getWidth())
					&& (mouseY >= cards[i].getRPositionY() && mouseY <= cards[i]
							.getRPositionY() + cards[i].getImage().getHeight())) {
				idx_focus = i; // If that the card then OK highlight it
			}

		}
		
		if(idx_focus >= 0) {				// If there is card then
			cards[idx_focus].getfocus();	// highlight it
		}
	}
	
	// The Action in PLAYING mode if you want to choose a card
	public void chooseCard() {
		for(int i = 0; i < cards.length; i++) {
			if(cards[i].isfocus()) {		// If that's the card is highlighting, then choose it
				if(!cards[i].isChosen()) {	// if it's not chosen then 
					cards[i].getChosen();	// choose it
				}
				else {						// otherwise
					cards[i].loseChosen();	// unselected it
				}
				sclick.play();
			}
			else {							// If that card is not highlight
				if(cards[i].isChosen()) {	// and it is chosen
					cards[i].loseChosen();	// then unselected it too
				}
			}
		}
	}
	
	// The action move a card to a position - Because it not easy to use then
	// do it wisely. Use this when you want to move in free direction
	public void moveCardTo(int idx, float x, float y, float speed) {
		if(idx == -1) {
			return;
		}
		float deltax = x - cards[idx].getPositionX();	// calculate the distance
		float deltay = y - cards[idx].getPositionY();
		
		if(Math.abs(deltax) >= speed) {		
			if(deltax > 0) {
				cards[idx].setPosition(cards[idx].getPositionX() + speed, cards[idx].getPositionY());	// move it here
			}
			else {
				cards[idx].setPosition(cards[idx].getPositionX() - speed, cards[idx].getPositionY());	// and here too
			}
		}
		else {
			cards[idx].setPosition(cards[idx].getPositionX() + deltax, cards[idx].getPositionY());	// this too
		}
		
		// same here
		if(Math.abs(deltay) >= speed) {			
			if(deltay > 0) {
				cards[idx].setPosition(cards[idx].getPositionX(), cards[idx].getPositionY() + speed);
			}
			else {
				cards[idx].setPosition(cards[idx].getPositionX(), cards[idx].getPositionY() - speed);
			}
		}
		else {
			cards[idx].setPosition(cards[idx].getPositionX(), cards[idx].getPositionY() + deltay);
		}
	}
	
	public void inactivePass() {
		for(int i = 0; i < cards.length; i++) {
			if(!cards[i].isChosen()) {
				cards[i].inactive();
			}
		}
	}
	
	public void choosePass() {
		for(int i = 0; i < cards.length; i++) {
			if(cards[i].isfocus()) {		// If that's the card is highlighting, then choose it
				if(!cards[i].isChosen()) {	// if it's not chosen then 
					cards[i].getChosen();	// choose it
					int j;
					for(j = 0; j < 3; j++) {
						if(pass[j] == 0) {
							pass[j++] = cards[i].getValue() * 10 + cards[i].getType();
							break;
						}
					}
					
					if(j == 3) {
						inactivePass();
					}
				}
				else {						// otherwise
					cards[i].loseChosen();	// unselected it
					for(int j = 0; j < 3; j++) {
						if(pass[j] == cards[i].getValue()*10+cards[i].getType()) {
							pass[j] = 0;
						}
					}
					for(int j = 0; j < cards.length; j++) {
						cards[j].active();
					}
				}
			}			
		}
	}
	
	private boolean compareCard(int a, int b) {
		int a1 = a/10;
		int a2 = a%10;
		
		int b1 = b/10;
		int b2 = b%10;
		
		if(a2 > b2) {	// Compare card type 
			return true;
		}
		
		if((a2 == b2) && (a1 > b1)) {	// Compare card number
			return true;
		}
		
		return false;
	}
	
	
	private void BubbleSort() {
		boolean swapped = true;
		int j = 0;
		Card tmp;
		while (swapped) {
			swapped = false;
			j++;
			for (int i = 0; i < cards.length - j; i++) {
				if (compareCard(cards[i].getValue()*10 + cards[i].getType(), cards[i+1].getValue()*10+cards[i+1].getType())) {
					tmp = cards[i];
					cards[i] = cards[i + 1];
					cards[i + 1] = tmp;
					swapped = true;
				}
			}
		}
	}
	
	public void setDirection(int direction) {
		this.direction = direction;
	}
	
	public String getpass() {
		String s = "";
		for(int i = 0; i < pass.length; i++) {
			s += "<card value='" + pass[i] +  "'/>";
		}
		return s;
	}
	
	public void pass() {
		for(int i = 0; i < cards.length; i++) {
			if(cards[i].isChosen()) {
				switch (number_chair) {
				case 0:
					switch (direction) {
					case 0:
						if (!isCardMove(i, 80, 250)) {
							moveCardTo(i, 80, 250, 1);
							cards[i].addrotate(1);
						}
						else {
							cur_state = PLAYING;
						}
						break;
					case 1:
						if (!isCardMove(i, 720, 250)) {
							moveCardTo(i, 720, 250, 1);
							cards[i].addrotate(-1);
						}
						else {
							cur_state = PLAYING;
						}
						break;
					case 2:
						if (!isCardMove(i, 400, 80)) {
							moveCardTo(i, 400, 80, 1);
						}
						else {
							cur_state = PLAYING;
						}
						break;
					}
					break;
				case 1:
					switch (direction) {
					case 0:
						if (!isCardMove(i, 400, 80)) {
							moveCardTo(i, 400, 80, 1);
							cards[i].addrotate(1);
						}
						else {
							cur_state = PLAYING;
						}
						break;
					case 1:
						if (!isCardMove(i, 400, 540)) {
							moveCardTo(i, 400, 540, 1);
							cards[i].addrotate(-1);
						}
						else {
							cur_state = PLAYING;
						}
						break;
					case 2:
						if (!isCardMove(i, 720, 250)) {
							moveCardTo(i, 720, 250, 1);
						}
						else {
							cur_state = PLAYING;
						}
						break;
					}
					break;
				case 2:
					switch (direction) {
					case 0:
						if (!isCardMove(i, 720, 250)) {
							moveCardTo(i, 720, 250, 1);
							cards[i].addrotate(1);
						}
						else {
							cur_state = PLAYING;
						}
						break;
					case 1:
						if (!isCardMove(i, 80, 250)) {
							moveCardTo(i, 80, 250, 1);
							cards[i].addrotate(-1);
						}
						else {
							cur_state = PLAYING;
						}
						break;
					case 2:
						if (!isCardMove(i, 400, 540)) {
							moveCardTo(i, 400, 540, 1);
						}
						else {
							cur_state = PLAYING;
						}
						break;
					}
					break;
				case 3:
					switch (direction) {
					case 0:
						if (!isCardMove(i, 400, 540)) {
							moveCardTo(i, 400, 540, 1);
							cards[i].addrotate(1);
						}
						else {
							cur_state = PLAYING;
						}
						break;
					case 1:
						if (!isCardMove(i, 400, 80)) {
							moveCardTo(i, 400, 80, 1);
							cards[i].addrotate(-1);
						}
						else {
							cur_state = PLAYING;
						}
						break;
					case 2:
						if (!isCardMove(i, 80, 250)) {
							moveCardTo(i, 80, 250, 1);
						}
						else {
							cur_state = PLAYING;
						}
						break;
					}
					break;
				}
			}
		}
	}
	
	public void setpass(int[] p) {
		//int t = 0;
		for(int i = 0; i < 3; i++) {
			for(int j = 0; j < number_card; j++) {
				if(cards[j].getValue()*10 + cards[j].getType() == p[i]) {
					cards[j].setChosen(true);
				}
			}
			pass[i] = p[i];
		}
	}
	
	public void autopass() {
		if(number_chair != 0) {
			Random r = new Random();
			int t1 = r.nextInt(13);
			int t2 = r.nextInt(13);
			if(t2 == t1) {
				t2 ++;
				if(t2 == 13) {
					t2 = 0;
				}
			}
			int t3 = r.nextInt(13);
			if(t3 == t1 || t3 == t2) {
				t3 ++;
				if (t3 == 13) {
					t3 = 0;
					if (t3 == t1 || t3 == t2) {
						t3++;
					}
				}
				if (t3 == t1 || t3 == t2) {
					t3++;
					if (t3 == 13) {
						t3 = 0;
						if (t3 == t1 || t3 == t2) {
							t3++;
						}
					}
				}
			}
			
			cards[t1].setChosen(true);
			cards[t2].setChosen(true);
			cards[t3].setChosen(true);
			
			pass[0] = cards[t1].getValue()*10 + cards[t1].getType();
			pass[1] = cards[t2].getValue()*10 + cards[t2].getType();
			pass[2] = cards[t3].getValue()*10 + cards[t3].getType();
		}
	}
	
	public boolean readyPass() {		
		for(int i = 0; i < 3; i++) {
			if(pass[i] == 0) {
				return false;
			}
		}
		return true;
	}
	
	public int[] getPass() {		
		return pass;
	}
	
	public void takePass(int[] arr) throws SlickException{
		int t = 0;
		for(int i = 0; i < cards.length; i++) {
			if(cards[i].isChosen()) {
				switch(number_chair) {
				case 0:				
					cards[i] = new Card(300, 520, arr[t]/10, arr[t]%10, 0, true);
					cards[i].up();
					break;
				case 1:
					cards[i] = new Card(80, 400, arr[t]/10, arr[t]%10, 90, true);					
					break;
				case 2:
					cards[i] = new Card(300, 80, arr[t]/10, arr[t]%10, 180, true);					
					break;
				case 3:
					cards[i] = new Card(720, 400, arr[t]/10, arr[t]%10, -90, true);					
					break;
				}
				t++;
			}
		}
		
		BubbleSort();
		for(int i = 0; i < number_card; i++) {
			cards[i].active();
			switch(number_chair) {
			case 0:
				cards[i].setPosition(100 + i*50, cards[i].getPositionY());
				break;
			case 1:
				cards[i].setPosition(cards[i].getPositionX(), 130 + i*20);
				break;
			
			case 2:
				cards[i].setPosition(520 - i*20, cards[i].getPositionY());
				break;
			
			case 3:
				cards[i].setPosition(cards[i].getPositionX(), 370 - i*20);
				break;
			
			}
		}		
	}
	
	// The Action set the deck back to normal after one lead
	private void normalizeDeck() {
		if(idx_lead != -1 && number_card > 0 && idx_lead != number_card) {
			Card temp = cards[idx_lead];
			
			for(int i = 0; i < number_card; i++) {
				if(i > idx_lead) {
					cards[i-1] = cards[i];
				}
			}
			
			cards[number_card-1] = temp;
			number_card --;
			
			for(int i = 0; i < number_card; i++) {
				switch(number_chair) {
				case 0:
					cards[i].setPosition(100 + (13-number_card)*25 + i*50, 540);
					break;
				case 1:
					cards[i].setPosition(80, 130 + (13-number_card)*10 + i*20);
					break;
				
				case 2:
					cards[i].setPosition(520 -(13-number_card)*10 - i*20, 80);
					break;
				
				case 3:
					cards[i].setPosition(720, 370 -(13-number_card)*10 - i*20);
					break;
				
				}
			}
		}
	}
	
	// Random card for AI to play
	private void autoPlay() {
		
		if(cards[0].getType() == 1 && cards[0].getValue() == 2) {
			idx_lead = 0;
			return;
		}
		
		for(int i = number_card-1; i >= 0; i--) {
			if(cards[i].getType() == GameplayState.getSuit()) {
				idx_lead = i;
				return;
			}			
		}
		
		Random random = new Random();
		int type = 0;
		if(GameplayState.is_heart_out) {
			type = random.nextInt(4) + 1;
		}
		else {
			type = random.nextInt(3) + 1;
		}
		
		int max = -1;
		int min = -1;
		
		for(int i = number_card-1; i >= 0; i--) {
			if(cards[i].type == type && max == -1) {
				max = i;
			}
			if(cards[i].type != type && max != -1) {
				min = i+1;
			}
		}
		
		if(min == -1) {
			min = 0;
		}
		if(max <= 0) {
			idx_lead = 0;
		}
		else {
			idx_lead = random.nextInt(max) + min;
		}
		
	}
	
	public boolean isleadready() {
		if(idx_lead == -1) {
			return false;
		}
		return true;
	}
	
	public void setleadcard(int c) {
		if(idx_lead == -1) {
			for(int i = 0; i < number_card; i++) {
				if(cards[i].getValue()*10 + cards[i].getType() == c) {
					idx_lead = i;
				}
			}
		}
	}
	
	public String getleadcard() {
		String s = "";
		if(idx_lead != -1) {
			int value = cards[idx_lead].getValue()*10 + cards[idx_lead].getType();
			s += "<card value='" + value + "'/>";
		}
		return s;
	}
	// The Action in PLAYING state when the Player is leading
	public void leadCard() {
		
		if(number_card == 0) {
			return;
		}
		
		for(int i = 0; i < number_card; i++) {			
			if(cards[i].isChosen() && idx_lead == -1) {		// find the chosen card to lead	
				idx_lead = i;				
			}
		}
		
		if(idx_lead == -1) {
			return;
		}
		
		float speed = 3;
		
		GameplayState.changeSuit(cards[idx_lead].getType());
		
		if(cards[idx_lead].getType() == 4) {
			GameplayState.heartOut();
		}
		
		for(int i = 0; i < number_card; i++) {	
			cards[i].active();
		}
		if(cards[idx_lead].isEnable()) {		// The leading card will need to disable
			cards[idx_lead].disable();
		}
		
		switch(number_chair) {
		case 0:
			if(!isCardMove(idx_lead, 400, 390)) {	// If the card is not arrive
				moveCardTo(idx_lead, 400, 390, speed);		// move it
			}
			else {									// otherwise
				normalizeDeck();						
				is_lead = false;					
				idx_lead = number_card;
			}
			break;
		case 1:		// same here
			if(!isCardMove(idx_lead, 310, 300)) {
				moveCardTo(idx_lead, 310, 300, speed);
				cards[idx_lead].up();
			}
			else {
				normalizeDeck();						
				is_lead = false;
				idx_lead = number_card;
			}
			break;
		case 2:		// same here
			if(!isCardMove(idx_lead, 400, 210)) {
				moveCardTo(idx_lead, 400, 210, speed);
				cards[idx_lead].up();
			}
			else {
				normalizeDeck();						
				is_lead = false;
				idx_lead = number_card;
			}
			break;
		case 3:		// and here
			if(!isCardMove(idx_lead, 490, 300)) {
				moveCardTo(idx_lead, 490, 300, speed);
				cards[idx_lead].up();
			}
			else {
				normalizeDeck();						
				is_lead = false;	
				idx_lead = number_card;
			}
			break;
		}
	}
	
	public void calTotal() {
		total_score += cur_score;
	}
	
	public void setPlay() {
		is_lead = true;
	}
	
	public void stopPlay() {
		is_lead = false;
	}
	
	public boolean isLead() {
		return is_lead;
	}
	
	public void endTrick() throws SlickException {
		cards[number_card].hide();		
		idx_lead = -1;
	}
	
	public int getCurState() {
		return cur_state;
	}
	
	public void setBeginGetCard() {
		
		cur_state = DEALING;
	}
	
	public int getLeadCardValue() {		
		return cards[idx_lead].getValue();		
	}
	
	public int getLeadCardType() {
		return cards[idx_lead].getType();
	}
	
	public void setWinner(int pos, int score) {
		win_chair = pos;
		
		if(number_chair == pos) {
			cur_score += score;
		}
	}
	
	public void shootTheMoon(boolean it) {
		if(it) {
			cur_score = 0;
		}
		else {
			cur_score = 26;
		}
	}
	
	public void setScore(int score) {
		total_score = score;
	}
	
	public int getCurScore() {
		return cur_score;
	}
	
	public int getTotalScore() {
		return total_score;
	}
	
	public boolean theFirstOne() {
		for(int i = 0; i < cards.length; i++) {
			if(cards[i].getType() == 1 && cards[i].getValue() == 2) {
				return true;
			}
		}
		
		return false;
	}
	
	public void moveToWinner(int pos) {
		cur_state = ENDTRICK;
		switch(pos) {
		case 0:
			if(!isCardMove(idx_lead, 400, 660)) {
				moveCardTo(idx_lead, 400, 660, 1);
			}
			else {
				cur_state = PLAYING;
			}
			break;
		case 1:
			if(!isCardMove(idx_lead, -60, 300)) {
				moveCardTo(idx_lead, -60, 300, 1);
			}
			else {
				cur_state = PLAYING;
			}
			break;
		case 2:
			if(!isCardMove(idx_lead, 400, -60)) {
				moveCardTo(idx_lead, 400, -60, 1);
			}
			else {
				cur_state = PLAYING;
			}
			break;
		case 3:
			if(!isCardMove(idx_lead, 860, 300)) {
				moveCardTo(idx_lead, 860, 300, 1);
			}
			else {
				cur_state = PLAYING;
			}
			break;
		}
	}
	
	public void render(GameContainer gameContainer, Graphics graphics, int state) throws SlickException {
		for(int i = 0; i < cards.length; i++) {
			cards[i].render(gameContainer, graphics);
		}
		
		//cards[cur_get_card].render(gameContainer, graphics);
		switch(number_chair) {
		case 0:
			unicodeFont.drawString(340, 465, name + ": " + Integer.toString(cur_score) + " (" + Integer.toString(total_score) + ")", Color.black);
			break;
		case 1:
			unicodeFont.drawString(20, 70, name + ": " + Integer.toString(cur_score) + " (" + Integer.toString(total_score) + ")", Color.black);
			break;
		case 2:
			unicodeFont.drawString(340, 5, name + ": " + Integer.toString(cur_score) + " (" + Integer.toString(total_score) + ")", Color.black);
			break;
		case 3:
			unicodeFont.drawString(660, 70, name + ": " + Integer.toString(cur_score) + " (" + Integer.toString(total_score) + ")", Color.black);
			break;
		}		
	}	
	
	
	public void update(GameContainer gameContainer, int delta, int state) throws SlickException {
		
		if(cur_state == 0 && state == DEALING) {
			cur_state = state;
		}
		
		if(state == DEALING ) {		
			
			
			if(cur_state == DEALING) {
				dealCard1();
				//count ++;
			}
			
		}	
		
		if(state == READY) {
			if(number_chair == 0) {
				Input input = gameContainer.getInput();
				
				focusCard(input.getMouseX(), input.getMouseY());
				
				if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
					choosePass();			
					//is_lead = true;		
				}	
			}
		}
		
		if(state == PASS) {
			pass();
		}
		
		// Player in playing mode
		if(state == PLAYING) {
			if(number_chair == 0) {
				Input input = gameContainer.getInput();			
				
				inactiveCard();
				focusCard(input.getMouseX(), input.getMouseY());
				
				if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
					chooseCard();			
					//is_lead = true;		
				}			
			}
			
			if(is_lead) {
				leadCard();	
				
				if(number_card == 0) {
					cur_state = END;
				}
			}			
		}	
		
		if(state == ENDTRICK) {
			endTrick();
		}
	}	

}
